#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define SERV_PORT 9527

void sys_err(const char *str)
{
    perror(str);
    exit(1);
}

int main(int argc, char *argv[])
{
	int cfd; //客户端的文件描述符
	int count = 10;
	char buf[4096];

	struct sockaddr_in serv_addr; //服务器的地址结构，注意是服务器的
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(SERV_PORT);//本地字节序转网络
	inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr.s_addr);//本地字节序转网络

	cfd = socket(AF_INET, SOCK_STREAM,0);
	if(cfd == -1){
		sys_err("client socket creat error");
	}

	int ret = connect(cfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
	if(ret == -1){
		sys_err("client connect error");
	}

	while(count--){
		write(cfd, "hello", 5);
		int fret = read(cfd, buf,sizeof(buf));
		write(STDOUT_FILENO, buf,fret);
		printf("\n");
		sleep(1);
	}

	close(cfd);

    return 0;
}
